<?php

namespace common\app;

use yii;

class Log
{
    //记录访问日志
    public function common($type, $data)
    {
        $debug = debug_backtrace();
        $_debug = [];
        for ($i = 1;$i<8;$i++)
            if(isset($debug[$i]['file']))
                $_debug[]=$debug[$i]['file'].' ： '.$debug[$i]['line'];
        $manager = \Cm::$app->user->getIdentity(); 
        if(empty($manager['id'])){
            $manager['id'] = 0;
            $openid = defined("WX_OPENDID") ? WX_OPENDID : '';
            if($openid != ''){
                $userInfo = \Cm::$db->one("SELECT id FROM user WHERE openid='{$openid}'");
                $manager['id'] = $userInfo['id'];    
            } 
        }   
        \Cm::$db->insert("sys_log", [
            "manager_id" => $manager['id'],
            "type" => $type,
            "msg" => is_array($data) ? json_encode($data) : $data,
            "backtrace"=>json_encode($_debug),
            "addtime" => date('Y-m-d H:i:s')
        ]);
    }

    private function etime()
    {
        list($t1, $t2) = explode(' ', microtime());
        $etime = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
        return $etime;
    }

    private function url()
    {
        $url = 'http://' . $_SERVER['SERVER_NAME'] . ($_SERVER["SERVER_PORT"] == 80 ? '' : ':' . $_SERVER["SERVER_PORT"]) . $_SERVER["REQUEST_URI"];
        $urlArr = explode('?', $url);
        $url = $urlArr[0];
        if (strtolower($url) == strtolower(BASE_URL . '/favicon.ico'))
            $url = "";

        $route = Yii::$app->controller->route;
        if (strtolower($route) == 'platform/main/error')
            $url = "";
        return $url;
    }

    public function ip()
    {
        $ip = ""; //getenv("REMOTE_ADDR");

        if (getenv("HTTP_CLIENT_IP"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if (getenv("HTTP_X_FORWARDED_FOR"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else if (getenv("REMOTE_ADDR"))
            $ip = getenv("REMOTE_ADDR");
        else $ip = "";

        $ips = explode(',', $ip);
        if (count($ips) > 0)
            $ip = $ips[0];

        return $ip;
    }

    private function req_get()
    {
        $route = Yii::$app->controller->route;
        $get = [];
        foreach ($_GET as $k => $p) {
            if (strtolower($k) != strtolower(yii\helpers\Url::to(["/{$route}"])))
                $get["{$k}"] = $p;
        }
        return json_encode($get);
    }

    /*
    *管理员日志
    */
    public function authlog($type,$info,$remark){
        $manager = \Cm::$app->user->getIdentity();
        if($type == 42){
            $manager['id'] = 0;
            $manager['username'] = '';
            if(empty($manager['id'])){
                $openid = defined("WX_OPENDID") ? WX_OPENDID : '';
                if($openid != ''){
                    $userInfo = \Cm::$db->one("SELECT id,username FROM sys_manager WHERE openid='{$openid}'");
                    $manager['id'] = $userInfo['id'];    
                    $manager['username'] = $userInfo['username'];    
                } 
            }else{
                $userInfo = \Cm::$db->one("SELECT id,username FROM sys_manager WHERE openid='{$manager['openid']}'");
                if(!empty($userInfo)){
                    $manager['id'] = $userInfo['id'];    
                    $manager['username'] = $userInfo['username']; 
                }   
            }   
        }    
        \Cm::$db->insert("auth_log", [
            "manager_id" => $manager['id'],
            "operator" => $manager['username'],
            "type" => $type,
            "info" => is_array($info) ? json_encode($info) : $info,
            "remark"=> is_array($remark) ? json_encode($remark) : $remark,
            "addtime" => date('Y-m-d H:i:s')
        ]);
    }

}

